package client.service;

import common.Message;
import common.MessageStatus;

import java.io.*;
import java.net.Socket;
import java.util.Date;

public class ChatService {
    public static final String storeFolder="A:\\IDEAJava\\QQClient\\store";

    private void chat(Message message,String getter,String sender,String content){
        //公共代码
        message.setGetter(getter);
        message.setSender(sender);
        message.setSend_time(new Date().toString());

        //在自己对话框显示自己发的内容
        if(content!=null&&content!="") {
            message.setContent(content);
            System.out.println(message);
        }else if(message.getFileName()!=null&&message.getFileName()!=""){
            System.out.println("\n"+message.getSend_time()+" to "+message.getGetter()+" "+message.getType()+"\n"+
                    message.getSender()+": 发送 "+message.getFileName()
            );
        }

        Socket socket = SocketThreadManager.getUserThread(sender).getSocket();
        try {
            ObjectOutputStream objOut= new ObjectOutputStream(socket.getOutputStream());
            objOut.writeObject(message);
            objOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void privateChat(String getter,String sender,String content){
        Message message = new Message();
        message.setType(MessageStatus.PRIVATE_CHAT);

        chat(message,getter,sender,content);
    }

    public  void groupChat(String getter,String sender,String content){
        Message message = new Message();
        message.setType(MessageStatus.GROUP_CHAT);

        chat(message,getter,sender,content);
    }

    public void sendFile(Message message,String getter,String sender,String fileName){
        message.setFileName(getRelativeFileName(fileName));
        message.setFileLength(new File(fileName).length());

        byte[] bytes=new byte[(int)message.getFileLength()];
        try {
            FileInputStream fileIn=null;
            try {
                fileIn= new FileInputStream(fileName);
                fileIn.read(bytes);
                message.setFileBytes(bytes);
            } finally {
                if(fileIn!=null){
                    fileIn.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        chat(message,getter,sender,null);
    }
    public void sendFilePrivately(String getter,String sender,String fileName){
        Message message = new Message();
        message.setType(MessageStatus.PRIVATE_SEND_FILE);
        sendFile(message,getter,sender,fileName);
    }
    public void sendFileToGroup(String getter,String sender,String fileName){
        Message message = new Message();
        message.setType(MessageStatus.GROUP_SEND_FILE);
        sendFile(message,getter,sender,fileName);
    }




    private String getRelativeFileName(String fileName){
        StringBuilder res = new StringBuilder();
        for (int i = fileName.length()-1; fileName.charAt(i)!='\\'; --i) {
            res.append(fileName.charAt(i));
        }
        return new String(res.reverse());
    }



}
